package com.sentinel.demo.pull.file.config;

import com.alibaba.csp.sentinel.adapter.servlet.CommonFilter;
import com.alibaba.csp.sentinel.annotation.aspectj.SentinelResourceAspect;
import lombok.Getter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Filter;

/**
 * 使用注解式Sentinel 配置
 * @author dsy on 2019/12/3
 */
@Configuration
//@PropertySource("classpath:sentinel.properties")
public class SentinelAspectConfiguration {

    @Getter
    @Value("${sentinel.demo.rull.flow.persistence.filePath}")
    private String persistenceFilePath;

    /**
     * 注解资源配置
     * @return
     */
    @Bean
    public SentinelResourceAspect sentinelResourceAspect() {
        return new SentinelResourceAspect();
    }

    /**
     * web servlet适配
     * @return
     */
    @Bean
    public FilterRegistrationBean sentinelFilterRegistration() {
        FilterRegistrationBean<Filter> registration = new FilterRegistrationBean<>();
        registration.setFilter(new CommonFilter());
        registration.addUrlPatterns("/*");
        registration.setName("sentinelFilter");
        registration.setOrder(1);
        return registration;
    }

    /**
     * 拉模式：使用文件配置规则（基于文件的数据）
     * @return
     */
//    @Bean
//    public AutoRefreshRule intRule(){
//        return new AutoRefreshRule(AutoRefreshRule.getFlowRuleListParser());
//    }
}
